import 'package:merchants/services/base-service.dart';

class RoleService extends BaseService {
  RoleService({required super.ref});

  Future<List<Map<String,dynamic>>> fetchRoles() async {
    final resp = await get<Map<String,dynamic>>('/roles', parser: (json) => json);
    return (resp['data'] as List).cast<Map<String,dynamic>>();
  }

  Future<int> createRole({required String name, List<int> permissionIds = const []}) async {
    final resp = await post<Map<String,dynamic>>('/roles', body: {
      'name': name,
      'permissionIds': permissionIds,
    }, parser: (j)=>j);
    return resp['id'] as int;
  }

  Future<void> updateRole(int id,{String? name,List<int>? permissionIds}) async {
    await put('/roles/$id', body: {
      if(name!=null) 'name': name,
      if(permissionIds!=null) 'permissionIds': permissionIds,
    }, parser: (j)=>j);
  }

  Future<void> deleteRole(int id) async {
    await delete('/roles/$id', parser: (j)=>j);
  }
} 